﻿
//zhongrui added at 2016.2.22
var uiHelper = {
    //显示等待窗口
    showLoading: function () {
        this._loading = layer.load(1, {
            shade: [0.1, '#fff'] //0.1透明度的白色背景
        });
    },
    //关闭等待窗口
    closeLoading: function () {
        layer.close(this._loading);
    },
    //弹出消息提示
    showMessage: function (msgStr) {
        this._message = layer.msg(msgStr);
    },
    //关闭消息提示
    closeMessage: function () {
        layer.msg(this._message);
    },
    //确认弹窗
    confirm: function (text, confirmCallback, confirmText, cancelText) {
        if (!confirmText) {
            confirmText = "确定";
        }
        if (!cancelText) {
            cancelText = "取消";
        }
        layer.confirm(text, { btn: [confirmText, cancelText] }, confirmCallback);
    },
    //警告弹窗
    alert: function (text, confirmCallback) {
        layer.confirm(text, { btn: ["确定"] }, confirmCallback);
    },
    ajaxResultCallback: function (result) {
        if (result.Messages && result.Messages instanceof Array) {
            if (result.Success === true) {
                var msgStr = "";
                for (var i = 0 ; i < result.Messages.length; i++) {
                    if (i !== 0) {
                        msgStr += "<br/>";
                    }
                    msgStr += result.Messages[i]
                }
                uiHelper.showMessage(msgStr);
            }
            else {
                var msgStr = "";
                for (var i = 0 ; i < result.Messages.length; i++) {
                    if (i !== 0) {
                        msgStr += "<br/>";
                    }
                    msgStr += result.Messages[i]
                }
                uiHelper.showMessage(msgStr);
            }
        }

        var redirectUrl = "";
        if (result.ReloadPage) {
            redirectUrl = window.location.href;
        }
        else if (result.RedirectUrl) {
            redirectUrl = result.RedirectUrl;
        }

        if (redirectUrl) {
            setTimeout(function () {
                window.location.href = redirectUrl;
            }, 500);
        }
    }
};

//zhongrui added at 2016.2.18
var Custom = function () {

    //处理ajax链接
    var handleAjaxLink = function () {
        $(document).on("click", ".js-ajax-button", function () {
            var ajaxUrl = $(this).data("url");
            var ajaxMethod = $(this).data("method") ? $(this).data("method").toUpperCase() : "POST";

            uiHelper.showLoading();
            var ajaxOptions = {
                url: ajaxUrl,
                type: ajaxMethod,
                error: function () {
                    uiHelper.closeLoading();
                },
                success: function (result) {
                    uiHelper.closeLoading();
                    uiHelper.ajaxResultCallback(result);
                }
            };

            $.ajax(ajaxOptions);

            return false;
        })
    }

    //查询的处理
    var handleSearch = function () { 

        //重置按钮
        //var $searchFormResetButton = $("");
        $(document).on("click", ".js-search-form-reset-button", function () {
            var $searchForm = $("#js-search-form");
            $("input[type=text]", $searchForm).val("");
            $("select", $searchForm).each(function () {
                var $ele = $(this);
                $ele.val($ele.find("option:first").val());
                if ($ele.hasClass("select2")) {
                    $ele.trigger("change");
                }
            });
            return false;
        });
    };

    //表单处理
    var handleForm = function () {
        $(document).on("click", ".js-ajax-submit", function () {
            $(this).parents("form").submit();
            return false;
        });

        handleFormValidationAdditionalMethods();

        $(document).on("submit", ".js-ajax-form", function () {
            
            var $self = $(this);

            //验证前自定义扩展
            if (window.beforeAjaxFormValidate instanceof Function
                && false === window.beforeAjaxFormValidate()) {
                return false;
            }

            if (!$self.valid()) {
                return false;
            }

            //ckEditor插件更新数据
            if (window.CKEDITOR !== undefined && window.CKEDITOR.instances) {
                for (instance in CKEDITOR.instances)
                    CKEDITOR.instances[instance].updateElement();
            }

            //uEditor表单提交
            //UE.instants.ueditorInstant0.sync($(this).attr("id"))
            if (window.UE !== undefined && window.UE.instants) {
                for (instant in window.UE.instants) {
                    window.UE.instants[instant].sync();
                }
            }

            //提交前自定义扩展
            if (window.beforeAjaxFormSubmit instanceof Function
                && false === window.beforeAjaxFormSubmit()) {
                return false;
            }

            var confirmAttach = {};
            if (window.getConfirmAttachData instanceof Function) {
                confirmAttach = window.getConfirmAttachData();
            }

            var formUrl = $self.attr("action");
            var formType = $self.attr("method");
            var formMimeType = $self.attr("enctype");
            var formData = new FormData(this);

            uiHelper.showLoading();
            var ajaxOptions = {
                url: formUrl,
                type: formType,
                data: formData,
                processData: false,
                contentType: false,
                error: function () {
                    uiHelper.closeLoading();
                },
                success: function (result) {

                    uiHelper.closeLoading();

                    if (formMimeType) {
                        result = JSON.parse(result);
                    }

                    uiHelper.ajaxResultCallback(result);
                }
            };

            if (formMimeType) {
                ajaxOptions.mimeType = formMimeType;
            }

            $.ajax(ajaxOptions);

            return false;
        });
    };

    //验证函数
    var handleFormValidationAdditionalMethods = function () {

        jQuery.validator.addMethod("integer", function (value, element) {
            return this.optional(element) || /^-?\d+$/.test(value);
        }, "请输入正确的整数");

        jQuery.validator.addMethod("decimal", function (value, element) {
            return this.optional(element) || /^\d+(\.\d{0,3})?$/.test(value);
        }, "情输入正确的金额");
    };

    return {
        init: function () {
            handleSearch();
            handleForm();
            handleAjaxLink();
        }
    };
}();
